package com.ycl.bookstore.controller;

import com.ycl.bookstore.DTO.BuyBookDTO;
import com.ycl.bookstore.common.Result;
import com.ycl.bookstore.service.BooksService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/books")
public class BookController {

    @Resource
    private BooksService booksService;

    @GetMapping("/list")
        public Result getListByPage(@RequestParam(value = "title",required = false) String title,
                                    @RequestParam(value = "author",required = false) String author,
                                    @RequestParam("pageNo") Long pageNo,
                                    @RequestParam("pageSize") Long pageSize){

        return booksService.getListByPage(title,author,pageNo,pageSize);
    }

    @GetMapping("/{id}")
    public Result getBookDetail(@PathVariable("id") Long id){
        return booksService.getBookById(id);
    }

    /**
     * 购买书籍
     * @param buyBookDTO
     * @return
     */
    @PutMapping("/buy")
    public Result buyBook(@RequestBody BuyBookDTO buyBookDTO){
        System.out.println(buyBookDTO);
        return booksService.buyBook(buyBookDTO);
    }

    /**
     * 我的书籍管理
     * @param title
     * @return
     */
    @GetMapping("/me")
    public Result getMyBook(@RequestParam(value = "title",required = false) String title){
        return booksService.getMyBook(title);
    }


    @PutMapping("/myBook")
    public Result createUserBook(@RequestParam("id") Long id,
                                 @RequestParam("num") Long num){
        return booksService.createUserBook(id,num);
    }
}
